package com.qf.base.result;

import com.alibaba.fastjson.JSONObject;

/**
 * 封装返回的json的格式
 *
 */
public class R {
    private int code;
    private String msg;
    private Object result;
    private Long count;

    public Long getCount() {
        return count;
    }

    public void setCount(Long count) {
        this.count = count;
    }

    public int getCode() {
        return code;
    }

    public R setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public R setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Object getResult() {
        return result;
    }

    public R setResult(Object result) {
        this.result = result;
        return this;
    }
    public static R success(){
        R r=new R();
        r.code=200;
        r.msg="请求成功";
        return r;
    }
    public static R success(String s){
        R r=new R();
        r.code=200;
        r.msg=s;
        return r;
    }
    public static R success(JSONObject s){
        R r=new R();
        r.code=200;
        r.msg=s.toJSONString();
        return r;
    }

    public static  R ok(){
        return success();
    }

    public static R ok(int code){
        R r=new R();
        r.code=code;
        r.msg="请求成功";
        return r;
    }
    public static R ok(Object o,Long count){
        R r=new R();
        r.code=200;
        r.msg="请求成功";
        r.result=o;
        r.count=count;
        return r;
    }
    public static R ok(String msg){
        R r=new R();
        r.code=200;
        r.msg=msg;
        return r;
    }
    public static R ok(int code,String msg){
        R r=new R();
        r.code=code;
        r.msg=msg;
        return r;
    }
    public static R ok(Object o){
        R r=new R();
        r.code=200;
        r.msg="请求成功";
        r.result=o;
        return r;
    }


    public static R error(){
        R r=new R();
        r.code=500;
        r.msg="请求错误";
        return r;
    }

    public static R error(int code){
        R r=new R();
        r.code=code;
        r.msg="请求错误";
        return r;
    }
    public static R error(String msg){
        R r=new R();
        r.code=500;
        r.msg=msg;
        return r;
    }
    public static R error(int code,String msg){
        R r=new R();
        r.code=code;
        r.msg=msg;
        return r;
    }
    public static R error(Object o){
        R r=new R();
        r.code=500;
        r.msg="请求错误";
        r.result=o;
        return r;
    }

    public String toJsonString(R r){
        return JSONObject.toJSONString(r);
    }
    @Override
    public String toString(){
        return toJsonString(this);
    }

}
